const { DataTypes } = require('sequelize');
const dayjs = require('dayjs');

const sequelize = require('../seq');

const Task = sequelize.define('Task', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  content: {
    type: DataTypes.STRING(500),
    allowNull: false
  },
  isCompleted: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false
  },
  remindTime: {
    type: DataTypes.DATE,
    set(value) {
      if (value) {
        const formatTime = dayjs(value).format('YYYY-MM-DD HH:mm:00');
        value = new Date(formatTime);
      }
      this.setDataValue('remindTime', value);
    },
    validate: {
      isAfterNow(value) {
        if(dayjs(value).isSameOrBefore(dayjs(), 'minute')){
          throw new Error('Remind time cannot be passed');
        }
      }
    }
  }
}, {
  underscored: true,
  paranoid: true
});

module.exports = Task;
